using System.Collections;
using UnityEngine;

public class ScriptHuman : MonoBehaviour
{
	private ScriptHumanState currentState;

	[SerializeField]
	private PlayerMecanimWrapper mecanimWrapper;

	[SerializeField]
	private GameObject weaponHolder;

	[SerializeField]
	private string weaponKey;

	private GameObject weaponObject;

	private WeaponMecanimWrapper weaponMecanimWrapper;

	private BaseWeapon weaponScript;

	[SerializeField]
	private float timeBetweenShots = 1f;

	[SerializeField]
	private float reloadTime = 1f;

	[SerializeField]
	private bool dispersionShot = true;

	[SerializeField]
	private float dispersionValue = 0.2f;

	private Transform target;

	[SerializeField]
	private int skinNumber;

	private Rigidbody rig;

	private MobNavigationController mobNavigationController;

	public Animator tonyAnimator;

	private bool lockAtPlayer = true;

	private float timeAfterShot;

	private Coroutine CallPlayerAnimation;

	private void Awake()
	{
		if (skinNumber >= 0)
		{
			SetSkin(skinNumber);
		}
		rig = GetComponent<Rigidbody>();
		rig.isKinematic = true;
		mecanimWrapper = GetComponentInChildren<PlayerMecanimWrapper>();
	}

	private void Update()
	{
		switch (currentState)
		{
		case ScriptHumanState.Shooting:
			if (timeAfterShot > timeBetweenShots)
			{
				Shot();
				timeAfterShot = 0f;
			}
			timeAfterShot += Time.deltaTime;
			break;
		case ScriptHumanState.MoveTarget:
			if (Time.frameCount % 3 != 0)
			{
				break;
			}
			if (Vector3.Distance(base.transform.position, target.position) > 5f)
			{
				if (lockAtPlayer)
				{
					mobNavigationController.StartMoving(1.5f);
					mecanimWrapper.currentVelocuty = 0.5f;
					mecanimWrapper.isMobMoving = true;
					mobNavigationController.GoToTarget(target, 0.1f);
				}
				lockAtPlayer = false;
			}
			else
			{
				if (!lockAtPlayer)
				{
					mobNavigationController.DisableNavigation();
					Stop();
				}
				lockAtPlayer = true;
			}
			break;
		}
		if (lockAtPlayer)
		{
			base.transform.LookAt(GameController.instance.playerScript.transform);
			base.transform.localEulerAngles = new Vector3(0f, base.transform.localEulerAngles.y, 0f);
		}
	}

	public void SetState(ScriptHumanState state)
	{
		currentState = state;
		switch (currentState)
		{
		case ScriptHumanState.Dead:
			break;
		case ScriptHumanState.StandUp:
			mecanimWrapper.StandUp();
			break;
		case ScriptHumanState.DropDown:
			mecanimWrapper.DropDown();
			break;
		case ScriptHumanState.Shooting:
			break;
		}
	}

	private void SetSkin(int number)
	{
		StartCoroutine(LoadAndSetSkin(number));
	}

	private IEnumerator LoadAndSetSkin(int number)
	{
		string skinName2 = "shablon_";
		if (number <= 9)
		{
			skinName2 += "0";
		}
		skinName2 += number;
		ResourceRequest resourse = Resources.LoadAsync<Texture2D>("Skins/" + skinName2);
		yield return resourse;
		GameController.SetMaterialAndTextureForEnemy(mecanimWrapper.gameObject, resourse.asset as Texture2D, null);
	}

	public void ShowWeapon()
	{
		if (!weaponObject)
		{
			StartCoroutine(LoadAndSetWeapon());
		}
	}

	public void HideWeapon()
	{
		if ((bool)weaponObject)
		{
			weaponObject.SetActive(false);
		}
	}

	private void MobDead()
	{
		SetState(ScriptHumanState.Dead);
		HideWeapon();
	}

	private IEnumerator LoadAndSetWeapon()
	{
		if (weaponHolder != null)
		{
			if (string.IsNullOrEmpty(weaponKey))
			{
				weaponKey = "eagl";
			}
			ResourceRequest weapon = Resources.LoadAsync("Weapons/" + weaponKey);
			yield return weapon;
			weaponObject = (GameObject)Object.Instantiate(weapon.asset as GameObject, weaponHolder.transform.position, weaponHolder.transform.rotation);
			weaponMecanimWrapper = weaponObject.GetComponent<WeaponMecanimWrapper>();
			weaponScript = weaponObject.GetComponent<BaseWeapon>();
			weaponObject.name = weaponKey + " (inst)";
			weaponObject.transform.SetParent(weaponHolder.transform);
			weaponObject.transform.localPosition = Vector3.zero;
			weaponObject.transform.localRotation = Quaternion.identity;
			weaponMecanimWrapper.Init(mecanimWrapper, reloadTime, timeBetweenShots);
			mecanimWrapper.SetWeapon(weaponMecanimWrapper.overrideController, reloadTime, timeBetweenShots);
		}
	}

	private void Shot()
	{
		weaponMecanimWrapper.Shoot();
		weaponScript.charge.ShowCharge(weaponScript.pointForShoot.position, GetPointShoot());
	}

	private Vector3 GetPointShoot()
	{
		Vector3 result = GameController.instance.playerScript.transform.position + Vector3.up * 1.2f;
		if (dispersionShot)
		{
			result += new Vector3(Random.Range(0f, dispersionValue), Random.Range(0f, dispersionValue), Random.Range(0f, dispersionValue));
		}
		return result;
	}

	public void SetTarget(Transform tr)
	{
		target = tr;
		mobNavigationController = GetComponent<MobNavigationController>();
		mecanimWrapper.mobVelocity = 5f;
		mecanimWrapper.StartCheck(GetComponentInChildren<SkinnedMeshRenderer>());
	}

	public void Stop()
	{
		mecanimWrapper.currentVelocuty = 0f;
		mecanimWrapper.isMobMoving = false;
	}

	public void ToggleCallPlayerAnimation(bool value)
	{
		if (value)
		{
			CallPlayerAnimation = StartCoroutine(CallPlayerCorutine());
		}
		else if (CallPlayerAnimation != null)
		{
			StopCoroutine(CallPlayerAnimation);
		}
	}

	private IEnumerator CallPlayerCorutine()
	{
		while (true)
		{
			tonyAnimator.SetTrigger("Demo");
			yield return new WaitForSeconds(5f);
		}
	}
}
